const pool = require('../config/db');

exports.getAllDesserts = async (req, res) => {
  try {
    const [rows] = await pool.query(`
      SELECT d.*, c.name AS category_name, s.name AS section_name 
      FROM desserts d
      LEFT JOIN categories c ON d.category_id = c.id
      LEFT JOIN sections s ON d.section_id = s.id
    `);
    res.json(rows);
  } catch (err) {
    res.status(500).json({ message: err.message });
  }
};

exports.createDessert = async (req, res) => {
  const { name, price, description, category_id, section_id } = req.body;
  
  try {
    const [result] = await pool.query(
      'INSERT INTO desserts (name, price, description, category_id, section_id) VALUES (?, ?, ?, ?, ?)',
      [name, price, description, category_id, section_id]
    );
    
    const [newDessert] = await pool.query(
      'SELECT * FROM desserts WHERE id = ?',
      [result.insertId]
    );
    
    res.status(201).json(newDest[0]);
  } catch (err) {
    res.status(400).json({ message: err.message });
  }
};